﻿using System;
using System.Linq;
using BankCreditPortfolio.DAL.Entities;

namespace BankCreditPortfolio.DAL.Services
{
    public class PaymentsService : DataService<Payments>
    {
        public IQueryable<Payments> GetReceived()
        {
            var filteredQuery =
                this.CreateQuery().Where(x => x.Payment.PaymentStatus.PaymentStatusId == 2).OrderByDescending(
                    x => x.Payment.Date);
            return filteredQuery.Count() > 0 ? filteredQuery : null;
        }

        public decimal GetAmountByPeriod(DateTime start, DateTime stop)
        {
            var filteredQuery =
                this.CreateQuery().Where(x => x.Payment.PaymentStatus.PaymentStatusId == 2 && x.Payment.Date >= start.Date && x.Payment.Date < stop.Date);
            decimal summ = 0;
            if (filteredQuery.Count() > 0)
            {
                foreach (var agreement in filteredQuery)
                {
                    switch (agreement.Payment.Currency.Title)
                    {
                        case "BYR":
                            summ += agreement.Payment.Amount / 8700;
                            break;
                        case "EURO":
                            summ += agreement.Payment.Amount * (decimal)1.56;
                            break;
                        case "RUR":
                            summ += agreement.Payment.Amount / 30;
                            break;
                        default:
                            summ += agreement.Payment.Amount;
                            break;
                    }
                }
            }
            return summ;
        }
    }
}
